'use strict'

const { ApiController } = require('jax')

class Controller extends ApiController {
  // 城市列表
  async index() {
    const { ctx } = this

    const results = await ctx.model.Store.aggregate([
      {
        $match: {
          isOpen: true,
        },
      },
      {
        $group: {
          _id: '$city',
          count: { $sum: 1 },
          lng: { $last: '$lng' },
          lat: { $last: '$lat' },
        },
      },
      { $sort: { count: -1 } },
    ])

    ctx.body = {
      code: 0,
      msg: 'success',
      data: results.map((item) => {
        return {
          city: item._id,
          lng: item.lng,
          lat: item.lat,
        }
      }),
    }
  }
}

module.exports = Controller
